package com.barber.util;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

public class XtUtil {
    public static void getRequestData(HttpServletRequest request, HashMap<String,String> map){
        Enumeration<String> params=request.getParameterNames();//枚举
        while (params.hasMoreElements()){
          String paramName=params.nextElement();
          map.put(paramName,request.getParameter(paramName));
        }

    }

 public static String toDatetimeString(String s){
     if(s==null){
         return s;
     }
     try{
         s=s.split("T")[0]+" "+s.split("T")[1];

     }catch (Exception e){

     }
return s;
 }
 public static String TimeConversion(String s){


             // 创建一个DateTimeFormatter用于解析ISO 8601格式的字符串
             DateTimeFormatter isoFormatter = DateTimeFormatter.ISO_INSTANT;

             // 将字符串解析为LocalDateTime（注意：这里我们实际上解析为Instant，然后转换为LocalDateTime）
             // 因为字符串中包含'Z'，它表示UTC时间，所以我们首先解析为Instant，然后转换为LocalDateTime
             LocalDateTime localDateTime = LocalDateTime.ofInstant(
                     java.time.Instant.parse(s),
                     java.time.ZoneId.of("UTC")
             );

             // 打印解析后的LocalDateTime
     System.out.println("Parsed LocalDateTime: " + localDateTime);

             // 创建一个DateTimeFormatter用于输出我们想要的格式
             DateTimeFormatter customFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS");

             // 使用自定义的formatter将LocalDateTime转换为字符串
             String formattedDateTime = localDateTime.format(customFormatter);


         return formattedDateTime;
     }

    public static int getInt(Scanner scanner) {
        int i = 0;
        while (scanner.hasNext()) {
            String s = scanner.nextLine();
            if (s.length() == 0) continue;

            try {
                i = Integer.parseInt(s);
                break;
            } catch (Exception e) {
                System.out.println("无效的整数");
            }
        }
        return i;
    }

    public static String getStr(Scanner scanner) {
        String s = null;
        while (scanner.hasNext()) {
            s = scanner.nextLine();
            if (s.length() == 0) continue;
            break;
        }
        return s;
    }
    //-1涓嶅瓨鍦� 鍏朵粬瀛樺湪

    public static boolean isPassStr(String pass) {
        if (pass.length() < 8 || pass.length() > 12) return false;

        boolean hasNumber = false,
                hasLetter = false;
        for (int i = 0; i < pass.length(); i++) {
            char ch = pass.charAt(i);
            if (ch >= '0' && ch <= '9') hasNumber = true;
            else if ((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z')) hasLetter = true;
            else return false;

        }
        return hasNumber && hasLetter;
    }

//    public static String TimeCon(String s) throws ParseException {
//        String dateTimeStr = s;
//        SimpleDateFormat isoFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
//        isoFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
//        Date date = isoFormat.parse(dateTimeStr);
//
//        SimpleDateFormat mysqlFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        mysqlFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
//        String mysqlDateTimeStr = mysqlFormat.format(date);
//
//
//        return mysqlDateTimeStr;
//    }
}
